package com.whfc.fuum.service.impl;

import com.whfc.common.enums.InviteResult;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.DateUtil;
import com.whfc.common.util.RandomUtil;
import com.whfc.fuum.dao.AppInviteCodeMapper;
import com.whfc.fuum.dto.WxInviteCodeDTO;
import com.whfc.fuum.dto.WxInviteDTO;
import com.whfc.fuum.dto.WxInviteUserDTO;
import com.whfc.fuum.dto.WxProjectDTO;
import com.whfc.fuum.entity.AppInviteCode;
import com.whfc.fuum.entity.SysDeptUser;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.manager.SysDeptManager;
import com.whfc.fuum.manager.SysUserManager;
import com.whfc.fuum.param.WxInviteUserJoinParam;
import com.whfc.fuum.service.AppInviteService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.Date;

/**
 * @ClasssName AppInviteServiceImpl
 * @Description 用户邀请相关服务
 * @Author hw
 * @Date 2020/12/25 15:26
 * @Version 1.0
 */
@DubboService(interfaceClass = AppInviteService.class, version = "1.0.0")
public class AppInviteServiceImpl implements AppInviteService {

    @Autowired
    private SysUserManager sysUserManager;

    @Autowired
    private AppInviteCodeMapper appInviteCodeMapper;

    @Autowired
    private SysDeptManager sysDeptManager;

    @Value("${mp.sys.invite.qrCodeUrl}")
    private String qrCodeUrl;

    @Override
    public WxInviteCodeDTO getCode(String phone, Integer deptId) {
        String code = RandomUtil.getRandomStr(16);
        Date expireTime = DateUtil.addDays(new Date(), 7);
        SysUser sysUser = sysUserManager.getUserByPhone(phone);
        AppInviteCode record = new AppInviteCode();
        record.setCode(code);
        record.setCreatorId(sysUser.getId());
        record.setCreatorName(sysUser.getNickname());
        record.setCreatorPhone(sysUser.getPhone());
        record.setDeptId(deptId);
        record.setExpireTime(expireTime);
        appInviteCodeMapper.insertSelective(record);

        WxInviteCodeDTO wxInviteCodeDTO = new WxInviteCodeDTO();
        wxInviteCodeDTO.setDeptId(deptId);
        wxInviteCodeDTO.setCode(code);
        wxInviteCodeDTO.setExpireTime(expireTime);
        wxInviteCodeDTO.setQrCode(String.format(qrCodeUrl, code));
        return wxInviteCodeDTO;
    }

    @Override
    public WxInviteDTO getInviteInfo(String phone, String code) {

        AppInviteCode appInviteCode = appInviteCodeMapper.selectByCode(code);
        if (appInviteCode == null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "邀请码错误");
        }

        if (appInviteCode.getExpireTime().before(new Date())) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "邀请码已过期");
        }

        WxInviteUserDTO wxInviteUserDTO = new WxInviteUserDTO();
        wxInviteUserDTO.setUserName(appInviteCode.getCreatorName());
        wxInviteUserDTO.setUserId(appInviteCode.getCreatorId());
        Integer deptId = appInviteCode.getDeptId();
        String deptName = sysDeptManager.getDeptNameById(deptId);
        WxProjectDTO wxProjectDTO = new WxProjectDTO();
        wxProjectDTO.setDeptId(deptId);
        wxProjectDTO.setDeptName(deptName);
        WxInviteDTO wxInviteDTO = new WxInviteDTO();
        wxInviteDTO.setProjectInfo(wxProjectDTO);
        wxInviteDTO.setResult(InviteResult.UNENROLLEDPROJECT.getValue());
        wxInviteDTO.setInviterUser(wxInviteUserDTO);
        SysUser sysUser = sysUserManager.getUserByPhone(phone);
        if (sysUser != null) {
            SysDeptUser sysDeptUser = sysDeptManager.getDeptUserByUserId(sysUser.getId(), deptId);
            if (sysDeptUser != null) {
                wxInviteDTO.setResult(InviteResult.ENROLLEDPROJECT.getValue());
            }
        }
        return wxInviteDTO;
    }

    @Override
    public void inviteUserJoin(String phone, String nickName, WxInviteUserJoinParam request) {
        Integer deptId = request.getDeptId();
        Integer inviteUserId = request.getInviteUserId();
        sysUserManager.saveInviterUserRule(nickName, phone, deptId, inviteUserId);
    }
}
